package www.wen.com.common.lib.data.network

import okhttp3.MultipartBody
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.http.*
import www.wen.com.common.lib.data.entities.*


/**
 * @author WEN
 * @Description:
 * @date 2020/11/25 10:55
 */
interface ApiService {

    @POST("db-api/general/sms/verify_code")
    suspend fun requestVerifyCode(
            @Body body: RequestVerifyBody,
    ): LazyEntity<VerifyCodeEntity>

    /**
     * 验证当前手机
     */
    @POST("db-api/user/users/verify/mobile")
    suspend fun requestVerifyMobile(
            @Body body: RequestBodyParameter,
    ): LazyEntity<String>

    /**
     * 换绑新手机号
     * 参数都是：mobile、smsCode
     */
    @POST("db-api/user/users/change/mobile")
    suspend fun requestChangeMobile(
            @Body body: RequestBodyParameter,
    ): LazyEntity<String>

    /**
     * type -> change_owner(转移服务器) 或者 kick(踢人)
     * 参数都是：serverId、userId
     */
    @POST("db-api/servers/member/{type}")
    suspend fun requestChangeOwnerOrKick(
            @Path("type") type: String,
            @Body body: RequestBodyParameter,
    ): LazyEntity<String>


    @GET("db-api/servers/channel/{channelId}")
    suspend fun requestChannelId(
            @Path("channelId") channelId: Long,
    ): LazyEntity<ChannelInfo>

    /**
     * 删除频道
     */
    @DELETE("db-api/servers/channel/{channelId}")
    suspend fun deleteChannel(
            @Path("channelId") channelId: Long,
    ): LazyEntity<String>

    /**
     * 删除分组
     */
    @DELETE("db-api/servers/group/{groupId}")
    suspend fun deleteGroup(
            @Path("groupId") groupId: String,
    ): LazyEntity<String>

    /**
     * 编辑分组
     */
    @PATCH("db-api/servers/group/{groupId}")
    suspend fun updateGroup(
            @Path("groupId") groupId: String,
            @Body body: RequestBodyParameter,
    ): LazyEntity<ServerGroupEntity.Content>

    /**
     * 编辑频道
     * 参数 - channelDesc、channelId、channelName、channelSort、groupId、topStatus
     */
    @PATCH("db-api/servers/channel/{channelId}")
    suspend fun patchChannel(
            @Path("channelId") channelId: Long,
            @Body body: RequestBodyParameter,
    ): LazyEntity<ChannelInfo>

    @GET("db-api/servers/member/invite/list")
    suspend fun requestInviteList(
            @Query("serverId") serverId: String,
            @Query("page") page: Int,
            @Query("size") size: Int,
    ): LazyEntity<InvitationEntity>

    /**
     * 短信登录
     * 参数 - mobile、smsCode
     */
    @POST("db-api/oauth/oauths/sms/login")
    suspend fun requestSmsLogin(
            @Body body: RequestRegisterBody,
    ): LazyEntity<LoginInfo>


    @POST("db-api/oauth/oauths/password/{type}")
    suspend fun requestRegister(
            //register|login
            @Path("type") type: String,
            @Body body: RequestRegisterBody,
    ): LazyEntity<LoginInfo>


    /**
     * 重置密码
     * 参数 - mobile、password、smsCode
     */
    @Headers("Content-Type:application/json;charset=UTF-8")
    @PUT("db-api/user/users/password")
    suspend fun requestResetPassword(
            @Body body: RequestBodyParameter,
    ): LazyEntity<UseInfoEntity>

    /**
     * 昵称精确搜索
     * @param nickName 昵称
     */
    @GET("db-api/user/users/info/{nickName}")
    suspend fun searchUserByNickName(
            @Path("nickName") nickName: String,
    ): LazyEntity<UseInfoEntity>

    /**
     * code换取accessToken
     * 参数 - code 微信code
     */
    @GET("db-api/oauth/oauths/wxmp/access_token")
    suspend fun requestAccessToken(
            @Query("code") code: String,
    ): LazyEntity<WxEntity>

    /**
     * token登录 | token注册
     * 参数 - code 微信code
     * @param type login|register
     */
    @POST("db-api/oauth/oauths/wxmp/{type}/by_token")
    suspend fun requestLoginOrRegisterByToken(
            @Path("type") type: String,
            @Body body: RequestLoginOrRegisterByToken,
    ): LazyEntity<EmptyEntity>


    /**
     * 绑定手机号码
     * 参数 - code 微信code
     * @param type login|register
     */
    @POST("db-api/oauth/oauths/wxmp/register/by_token")
    suspend fun requestRegisterByToken(
            @Body body: RequestLoginOrRegisterByToken,
    ): LazyEntity<LoginInfo>

    /**
     * 创建新的服务器
     */
    @POST("db-api/servers/server")
    suspend fun requestCreateServer(
            @Body body: RequestBodyParameter,
    ): LazyEntity<ServerEntity>

    /**
     * 上传logo
     */
    @Multipart
    @POST("db-api/general/oss/files")
    suspend fun requestUploadOssFile(
            @PartMap partMap: Map<String, @JvmSuppressWildcards okhttp3.RequestBody>,
            @Part vararg file: MultipartBody.Part,
    ): LazyEntity<UpOssFileEntity>

    /**
     * 首页-服务:左边垂直服务器
     */
    @GET("db-api/servers/server/self/list")
    suspend fun requestServerSelfList(
            @Query("page") page: Int,
            @Query("size") size: Int,
    ): LazyEntity<ServerSelfListEntity>

    /**
     * 首页-服务:右边服务器分组数据
     */
    @GET("db-api/servers/group/channel/tree/{serverId}")
    suspend fun requestServerGroupList(
            @Path("serverId") serverId: String,
    ): LazyEntity<ServerGroupEntity>


    /**
     * 首页- 推荐：把服务器加入预览
     */
    @GET("db-api/servers/server/preview/{serverId}")
    suspend fun requestServerPreview(
            @Path("serverId") serverId: String,
    ): LazyEntity<ServerSelfListEntity.Content>

    /**
     * 服务器删除
     */
    @DELETE("db-api/servers/server/{serverId}")
    suspend fun requestDeleteServer(
            @Path("serverId") serverId: String,
    ): LazyEntity<String>

    /**
     * 更新服务器信息
     */
    @PATCH("db-api/servers/server/{serverId}")
    suspend fun requestUpdateServerInfo(
            @Path("serverId") serverId: String,
            @Body body: RequestBodyParameter,
    ): LazyEntity<ServerUpdateInfoEntity>

    /**
     * 频道排序
     */
    @PATCH("db-api/servers/channel/batch")
    suspend fun requestBatch(
            @Body body: List<RequestBodyParameter>,
    ): LazyEntity<List<ChannelBatchEntity>>

    /**
     * 分组排序
     */
    @PATCH("db-api/servers/group/batch")
    suspend fun requestGroupSort(
            @Body body: List<RequestBodyParameter>,
    ): LazyEntity<List<ChannelBatchEntity>>

    /**
     * 创建频道
     */
    @POST("db-api/servers/channel")
    suspend fun requestCreateChannel(
            @Body body: RequestBodyParameter,
    ): LazyEntity<ChannelEntity>

    /**
     * 获取服务器详情
     */
    @GET("db-api/servers/server/{serverId}")
    suspend fun requestServerDetails(
            @Path("serverId") serverId: String,
    ): LazyEntity<ServerInfo>

    /**
     * 获取仅仅分组数据
     */
    @GET("db-api/servers/group/list/{serverId}")
    suspend fun requestServerGroup(
            @Path("serverId") serverId: String,
    ): LazyEntity<List<GroupingEntity>>


    /**
     * 创建新分组
     */
    @POST("db-api/servers/group")
    suspend fun requestCreateNewGroup(
            @Body body: RequestBodyParameter,
    ): LazyEntity<GroupingEntity>


    /**
     * 获取分享链接
     */
    @POST("db-api/servers/share")
    suspend fun requestServerShareUrl(
            @Body body: RequestBodyParameter,
    ): LazyEntity<ShareUrlEntity>


    /**
     * 获取服务器成员(使用协程)
     * @param serverId 服务器ID
     * @param nickName 昵称
     * @param page 分页.page
     * @param size 分页.size
     */
    @GET("db-api/servers/member/list")
    suspend fun requestServersMemberList(
            @Query("serverId") serverId: String,
            @Query("nickName", encoded = true) nickName: String?,
            @Query("page") page: Int,
            @Query("size") size: Int,
    ): LazyEntity<ServiceMemberListEntity>

    /**
     * 获取服务器成员(不使用协程)
     * @param serverId 服务器ID
     * @param nickName 昵称
     * @param page 分页.page
     * @param size 分页.size
     */
    @GET("db-api/servers/member/list")
    fun requestServersMemberList2(
            @Query("serverId") serverId: String,
            @Query("nickName", encoded = true) nickName: String?,
            @Query("page") page: Int,
            @Query("size") size: Int,
    ): Call<ServiceMemberListEntity2>


    /**
     * 批量获取用户在线状态
     * @param accids 用户的云信id
     */
    @POST("db-api/user/users/online")
    suspend fun requestUsersOnlineStatus(
            @Body accids: Array<String>,
    ): LazyEntity<List<UseInfoEntity>>

    /**
     *  invite - 被邀请加入服务器(拉人入群)
     *  add - 加入服务器(自己加入)
     * @param body -> serverId，userId
     * @param type -> invite|add
     */
    @POST("db-api/servers/member/{type}")
    suspend fun requestInviteOrAdd(
            @Path("type") type: String,
            @Body body: RequestBodyParameter,
    ): LazyEntity<String>

    /**
     *  编辑用户服务设置(昵称、通知设置、好友申请设置)
     * @param body -> allowFriendFlag、nickName、noticeType
     */
    @PATCH("db-api/servers/member/{id}")
    suspend fun requestMemberSetting(
            @Path("id") id: String,
            @Body body: RequestBodyParameter,
    ): LazyEntity<String>

    /**
     *  创建分享链接
     *  add - 加入服务器(自己加入)
     * @param body -> serverId，userId
     * @param type -> invite|add
     */
    @POST("db-api/servers/share")
    suspend fun requestInviteOrAdd(
            @Body body: RequestBodyParameter,
    ): LazyEntity<String>


    /**
     * 获取服务器列表
     * @param serverIdOrName 服务器ID或名称
     * @param recommendFlag 是否为推荐服务器（y-是; n-否）
     * @param page 分页.page
     * @param size 分页.size
     */
    @GET("db-api/servers/server/list")
    suspend fun requestServerList(
            @Query("serverIdOrName") serverIdOrName: String?,
            @Query("recommendFlag") recommendFlag: String?,
            @Query("page") page: Int?,
            @Query("size") size: Int?,
    ): LazyEntity<ServerListEntity>

    /**
     * 创建置顶消息
     * 参数 - channelId || teamId  msgTime、uuId
     */
    @POST("db-api/servers/channel/top_msg")
    fun addServerChannelTopMsg(
            @Body body: RequestServerChannelTopMsgBodyParameter,
    ): Call<String>

    /**
     * 获取频道置顶消息列表
     * @param channelId 频道的自增id，不是云信的群组ID
     */
    @GET("db-api/servers/channel/top_msg/{teamId}")
    suspend fun getServerChannelTopMsgList(
            @Path("teamId") teamId: String,
    ): LazyEntity<TopMsgEntity>

    /**
     * 删除频道置顶消息
     * @param uuid 置顶消息的uuid
     */
    @DELETE("db-api/servers/channel/top_msg/{uuid}")
    fun deleteServerChannelTopMsg(
            @Path("uuid") uuid: String,
    ): Call<String>

    /**
     * 启动页的接口
     * page
     * size
     */
    @GET("db-api/general/start_page")
    suspend fun requestStartPage(
            @Query("size") size: Int,
            @Query("page") page: Int,
    ): LazyEntity<StartPageEntity>

    /**
     * 获取用户信息
     * Authorization header
     */
    @GET("db-api/oauth/oauths/user/info")
    suspend fun requestUserInfo(): LazyEntity<UseInfoEntity>

    /**
     * 地区的
     * parentCode - 父级Code(code为 100000 时,获取省列表)
     * page
     * size
     */
    @GET("db-api/general/area")
    suspend fun requestArea(
            @Query("parentCode") parentCode: String,
    ): LazyEntity<CityEntity>

    /**
     * 更新信息
     */
    @PATCH("db-api/user/users/info")
    suspend fun requestUserInfo(
            @Body body: RequestBodyParameter,
    ): LazyEntity<UseInfoEntity>

    /**
     * 通过ID获取用户信息
     */
    @GET("db-api/user/users/{id}")
    suspend fun requestUserInfoById(
        @Path("id") id: String,
    ): LazyEntity<UseInfoEntity>

    /**
     * 通过链接加入时获取服务器信息
     */
    @POST("db-api/servers/share/find")
    suspend fun requestShareFind(
            @Body body: RequestBodyParameter,
    ): LazyEntity<JoinServerEntity>

    /**
     * 被邀请加入服务器(拉人入群)
     */
    @POST("db-api/servers/member/invite")
    suspend fun requestMemberInvite(
            @Body body: RequestBodyParameter,
    ): LazyEntity<String>

    /**
     * 退出服务器
     */
    @POST("db-api/servers/member/quit")
    suspend fun requestExitServers(
            @Query("serverId") serverId: String,
    ): LazyEntity<String>

    /**
     * 账号注销
     */
    @POST("db-api/user/users/remove")
    suspend fun requestRemove(): LazyEntity<String>

    /**
     * 获取版本更新信息
     */
    @GET("db-api/general/version/lastVersion")
    fun requestLastVersion(
            @Query("type") type: String,
            @Query("versionName") versionName: String,
    ): Call<String>


    /**
     * 获取各种信息
     * type - (帮助中心、用户协议、隐私政策、关于我们、邀请海报)
     * page
     * size
     */
    @GET("db-api/general/protocol")
    suspend fun requestProtocol(
            @QueryMap map: Map<String, String>,
    ): LazyEntity<ProtocolEntity>

    /**
     * 获取官方服务器
     */
    @GET("db-api/servers/server/official")
    suspend fun requestServerOfficial(): LazyEntity<ServerSelfListEntity.Content>

    /**
     * 销毁token
     */
    @GET("db-api/oauth/oauths/token-revoke")
    suspend fun requestTokenRevoke(): LazyEntity<String>


}


//const val BASIC_URL = "http://wekan.cn:7023/"
const val BASIC_URL = "https://api.dianbaozu.com/"


const val LAST_VERSION = "http://wekan.cn:7023/db-api/general/version/lastVersion"

//const val BASIC_IMAGE_URL = "http://figo-medu-cloud.oss-cn-shenzhen.aliyuncs.com/"
const val BASIC_IMAGE_URL = "https://dianbao-cloud.oss-cn-shenzhen.aliyuncs.com/"


